import moment from 'moment'
import request from '@/utils/request'

// 指标相关--start
// 指标分页
export function requestTargetPage(data) {
  if (data?.indexLabel?.constructor === Array) {
    data.indexLabel = data.indexLabel.join(',')
  }
  return request({
    url: '/jtx/index/page',
    method: 'post',
    loading: false,
    data: { ...data, hasAllData: false },
  })
}
export function requestJtxIndexCenterPage(data) {
  let url =
    data.collect === '收藏'
      ? '/matrix/collect/myCollectList'
      : data.collect === '我的'
      ? 'jtx/index/center/page/my'
      : 'jtx/index/center/page'
  delete data.collect
  return request({
    url: url,
    method: 'post',
    loading: false,
    data: { ...data },
  })
}
export function requestMatrixCollectMyCollectList(data) {
  return request({
    url: '/matrix/collect/myCollectList',
    method: 'post',
    loading: false,
    data: { ...data },
  })
}

// 指标发布分页
export function requestTargetPublishPage(data) {
  const params = {
    ...data,
    hasAllData: false,
  }

  if (data.publishTime && data.publishTime.length >= 2) {
    params.publishBeginDate = moment(params.publishTime[0]).format('YYYY-MM-DD 00:00:00')
    params.publishEndDate = moment(params.publishTime[1]).format('YYYY-MM-DD 23:59:59')
    delete params.publishTime
  }

  return request({
    url: '/jtx/index/publishPage',
    method: 'post',
    loading: false,
    data: params,
  })
}
// 指标目录分页
export function requestTargetCatalogPage(data) {
  return request({
    url: '/jtx/index/catalogPage',
    method: 'post',
    loading: false,
    data: { ...data, publishStatus: '1', hasAllData: true },
  })
}

// 指标发布状态更新
export function requestTargetPublishStatusUpdate(data) {
  return request({
    url: '/jtx/index/updateStatus',
    method: 'post',
    loading: false,
    data,
  })
}

export function requestTargetCreate(data) {
  if (data?.indexLabel?.constructor === Array) {
    data.indexLabel = data.indexLabel.join(',')
  }
  return request({
    url: '/jtx/index/save',
    method: 'post',
    loading: false,
    data,
  })
}
export function requestTargetUpdate(data) {
  if (data?.indexLabel?.constructor === Array) {
    data.indexLabel = data.indexLabel.join(',')
  }
  return request({
    url: '/jtx/index/update',
    method: 'post',
    loading: false,
    data,
  })
}
export function requestTargetDelete(data) {
  return request({
    url: '/jtx/index/delete',
    method: 'post',
    loading: false,
    data: { id: data.id },
  })
}

export function requestTargetDetail(params) {
  return request({
    url: '/jtx/index/get',
    method: 'get',
    loading: false,
    params,
  })
}
//指标相关--end

export function post(url, data, loading = false) {
  return request({
    url: url,
    method: 'post',
    loading: loading,
    data,
  })
}

export function tableField(id) {
  return request({
    url: '/dataset/table/getWithPermission/' + id,
    method: 'post',
    loading: false,
    hideMsg: true,
    timeout: 60000,
  })
}

export function getChartTree(data) {
  return request({
    url: 'api',
    method: 'post',
    loading: false,
    data,
  })
}

export function chartCopy(id, panelId) {
  return request({
    url: '/chart/view/chartCopy/' + id + '/' + panelId,
    method: 'post',
    loading: false,
  })
}

export function chartBatchCopy(params, panelId) {
  return request({
    url: '/chart/view/chartBatchCopy/' + panelId,
    method: 'post',
    data: params,
    loading: false,
  })
}

export function chartGroupTree(data) {
  return request({
    url: '/chart/group/tree',
    method: 'post',
    loading: false,
    data,
  })
}

export function searchAdviceSceneId(panelId) {
  return request({
    url: '/chart/view/searchAdviceSceneId/' + panelId,
    method: 'get',
    loading: false,
  })
}

export function checkSameDataSet(viewIdSource, viewIdTarget) {
  return request({
    url: '/chart/view/checkSameDataSet/' + viewIdSource + '/' + viewIdTarget,
    method: 'get',
    loading: false,
  })
}

export function ajaxGetDataOnly(id, panelId, data) {
  return request({
    url: '/chart/view/getData/' + id + '/' + panelId,
    method: 'post',
    loading: false,
    hideMsg: true,
    data,
  })
}

export function pluginTypes() {
  return request({
    url: '/plugin/view/types',
    method: 'post',
  })
}

export function deleteCircle(id) {
  return request({
    url: '/chart/group/deleteCircle/' + id,
    method: 'post',
    loading: true,
  })
}

export function getChartDetails(id, panelId, data) {
  return request({
    url: '/chart/view/get/' + id + '/' + panelId,
    method: 'post',
    loading: false,
    data,
  })
}

export function viewEditSave(panelId, data) {
  return request({
    url: '/chart/view/viewEditSave/' + panelId,
    method: 'post',
    loading: false,
    data,
  })
}

export function resetViewCacheCallBack(viewId, panelId, callback) {
  // 加载仪表盘组件视图数据
  resetViewCache(viewId, panelId).then((rep) => {
    callback(rep)
  })
}

export function resetViewCache(viewId, panelId) {
  return request({
    url: '/chart/view/resetViewCache/' + viewId + '/' + panelId,
    method: 'post',
    loading: false,
  })
}

export function checkTitle(data) {
  return request({
    url: '/chart/view/checkTitle',
    method: 'post',
    data: data,
    loading: false,
  })
}

export function viewPropsSave(panelId, data) {
  return request({
    url: '/chart/view/viewPropsSave/' + panelId,
    method: 'post',
    loading: false,
    data,
  })
}

export const viewOptions = (panelId) => {
  return request({
    url: '/chart/view/viewOptions/' + panelId,
    method: 'post',
  })
}

export function getDatasourceRelationship(id) {
  return request({
    url: `/api/relation/datasource/${id}`,
    method: 'get',
    loading: true,
  })
}

export function getDatasetRelationship(id) {
  return request({
    url: `/api/relation/dataset/${id}`,
    method: 'get',
    loading: true,
  })
}

export function getPanelRelationship(id) {
  return request({
    url: `/api/relation/panel/${id}`,
    method: 'get',
    loading: true,
  })
}

//保存指标审批
export function applySave(data) {
  return request({
    url: `/jtx/matrix/approve/save`,
    method: 'post',
    loading: true,
    data,
  })
}

//申请历史
export function getApplyHistory(data) {
  return request({
    url: `/jtx/matrix/approve/page`,
    method: 'post',
    loading: false,
    data,
  })
}

//审批详情
export function getApproveDetail(params) {
  return request({
    url: `/jtx/matrix/approve/get`,
    method: 'get',
    loading: true,
    params,
  })
}

//指标审批
export function approveUpdate(data) {
  return request({
    url: `/jtx/matrix/approve/updateStatus`,
    method: 'post',
    loading: true,
    data,
  })
}

//指标审批
export function approveBatchUpdateStatus(data) {
  return request({
    url: `/jtx/matrix/approve/batchUpdateStatus`,
    method: 'post',
    loading: true,
    data,
  })
}

//指标目录分页查询
export function catalogPage(data) {
  return request({
    url: `/jtx/index/catalogPage`,
    method: 'post',
    loading: true,
    data,
  })
}

//指标目录分页查询
export function publishPage(data) {
  return request({
    url: `/jtx/index/publishPage`,
    method: 'post',
    loading: true,
    data,
  })
}

//分享列表
export function sharePage(data) {
  return request({
    url: `/jtx/matrix/share/page`,
    method: 'post',
    loading: true,
    data,
  })
}

//保存指标分享
export function shareSave(data) {
  return request({
    url: `/jtx/matrix/share/save`,
    method: 'post',
    loading: true,
    data,
  })
}

//撤销分享
export function shareDel(data) {
  return request({
    url: `/jtx/matrix/share/delete`,
    method: 'post',
    loading: true,
    data,
  })
}

//分享详情
export function shareGet(params) {
  return request({
    url: `/jtx/matrix/share/get`,
    method: 'get',
    loading: true,
    params,
  })
}

//分享用户列表
export function shareUserList(params) {
  return request({
    url: `/jtx/matrix/share/shareUserList`,
    method: 'get',
    loading: true,
    params,
  })
}

//指标视图详情
export function getChartViewByPanelId(params) {
  return request({
    url: `/jtx/matrix/target/getChartViewByPanelId`,
    method: 'get',
    loading: true,
    params,
  })
}

// 指标标签
export function requestMatrixLabelPage(data) {
  return request({
    url: '/matrix/label/page',
    method: 'post',
    loading: false,
    data: { ...data },
  })
}

// 指标标签新增
export function requestMatrixLabelSave(data) {
  return request({
    url: '/matrix/label/save',
    method: 'post',
    loading: false,
    data: { ...data },
  })
}
// 指标标签修改
export function requestMatrixLabelUpdate(data) {
  return request({
    url: '/matrix/label/update',
    method: 'post',
    loading: false,
    data: { ...data },
  })
}
// 指标标签删除
export function requestMatrixLabelDelete(data) {
  return request({
    url: '/matrix/label/delete',
    method: 'post',
    loading: false,
    data: { ...data },
  })
}
//查询指标查看权限
export function requestCheckJtxMatrix(data) {
  return request({
    url: '/jtx/index/checkJtxMatrix',
    method: 'post',
    loading: false,
    data,
  })
}
//新增-修改指标查看权限
export function requestSaveOrUpdateJtxMatrix(data) {
  return request({
    url: '/jtx/index/saveOrUpdateJtxMatrix',
    method: 'post',
    loading: false,
    data,
  })
}
//修改指标查看权限
export function requestDelJtxMatrix(data) {
  return request({
    url: '/jtx/index/delJtxMatrix',
    method: 'post',
    loading: false,
    data,
  })
}
//新增-修改指标审批
export function requestSaveOrUpdateApproval(data) {
  return request({
    url: '/jtx/index/saveOrUpdateApproval',
    method: 'post',
    loading: false,
    data,
  })
}
//删除指标审批
export function requestDelApproval(data) {
  return request({
    url: '/jtx/index/delApproval',
    method: 'post',
    loading: false,
    data,
  })
}
//查询指标审批
export function requestGetApproval(data) {
  return request({
    url: '/jtx/index/getApproval',
    method: 'post',
    loading: false,
    data,
  })
}
